<?php
function proto_start($obj)
    {
      global $$obj;
      if (is_array($obj))foreach($obj as $o)proto_start($o);
      else $$obj=new $obj();
    }
/**
 * Класс для быстрого создания объектов на основе прототипа
 */
class proto {
	function proto()
	    {
	    $this->table=get_class($this);
	    $this->fields=array("id"=>array("type"=>"autoid","name"=>"id"),"ts"=>array("type"=>"autots","name"=>"дата"));
	    }
	function get($id) 
	    {
	    global $db;
	    return $db->fetchRow('SELECT * FROM ~'.$this->table.' WHERE `id`=$',$id);
	    }
	function getfield($id,$name) {
	    global $db;   
	    return $db->fetchOne("SELECT `$name` FROM ~".$this->table." WHERE `id`=$id");
	}
	function keys($where="1",$order="id",$limit="") {
	    global $db;
	    $sql="SELECT `id` FROM ~".$this->table." WHERE $where ORDER by $order";
	    if ($limit)$sql.=" LIMIT $limit";
	    $res=$db->fetchAll($sql);
	    foreach($res as $k=>$v) {
	      $res[$k]=$v['id'];
	    }
	    return $res;
	}
	function total($where="1") {
	    global $db;
	    return $db->fetchOne('SELECT COUNT(*) FROM ~'.$this->table.' WHERE '.$where);
	}
	function maxid($field="id",$where="1") {
	    global $db;
	    return $db->fetchOne("SELECT MAX($field) FROM ~".$this->table." WHERE $where");
	}
	function insert($data='') {
	    global $db;
	    if (empty($data)) $data=$_REQUEST;
	    foreach ($this->fields as $k=>$v) {
	      if($this->fields[$k]["type"]=="unixtime")$data[$k]=time();
	      if($this->fields[$k]["type"]=="checkbox"&&$data[$k]=="on")$data[$k]=1; 
	      if($this->fields[$k]["type"]=="seq")$data[$k]=$this->maxid("seq")+1;
	      if (isset($data[$k]) && $k!='id')
	        $insert[$k]=$data[$k];
	    }
	    return $db->insert($this->table,$insert);
	}
	function update() {
	    global $db;
	    $data=$_REQUEST;
	    foreach ($this->fields as $k=>$v) {
	      if ($this->fields[$k]["type"]=="checkbox" && $data[$k]=="on") $data[$k]=1;
	      if (isset($data[$k]) && $k!="id") $update[$k]=$data[$k];
	    }
	    return $db->update($this->table,$update,'`id`='.intval($_REQUEST["id"]));
	}
	function delete($id) {
	    global $db;
	    return $db->delete($this->table,$id);
	}
	function show_sql() {
	    global $db;
	    $types=array(
		    "autoid"=>"INT NOT NULL AUTO_INCREMENT PRIMARY KEY",
		    "autots"=>"TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP",
		    "seq"=>"INT UNSIGNED NOT NULL",
		    "name"=>"VARCHAR( 250 ) NOT NULL",
		    "select"=>"INT UNSIGNED NOT NULL",
		    "text"=>"VARCHAR( 250 ) NOT NULL",
		    "unixtime"=>"VARCHAR( 250 ) NOT NULL",
		    "date"=>"VARCHAR( 250 ) NOT NULL",
		    "checkbox"=>"TINYINT UNSIGNED NOT NULL",
		    "msword"=>"TEXT NOT NULL",
		    "image"=>"VARCHAR( 250 ) NOT NULL",
		    "textarea"=>"TEXT NOT NULL"
	    );
	    foreach ($this->fields as $k=>$v)$str[]="`$k` ".$types[$v["type"]];
	
	    $str=implode(",\n	",$str);
	    echo "CREATE TABLE `".$db->prefix.$this->table."` (\n	".
	          $str.
	          "\n".
	          ") ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci;";
	}
}

?>
